//
// Created by weiyixing on 25-9-11.
// Enhanced version with better error handling and Python bindings support
//

#ifndef ORBBECSDK_SAMPLES_SCVCAMERACORE_H
#define ORBBECSDK_SAMPLES_SCVCAMERACORE_H

#include <memory>
#include <vector>
#include <string>
#include <functional>
#include "libobsensor/hpp/Pipeline.hpp"
#include "libobsensor/hpp/StreamProfile.hpp"
#include "libobsensor/hpp/Filter.hpp"
#include "libobsensor/hpp/Device.hpp"
#include "opencv2/opencv.hpp"

typedef enum{
    MJPG = 0,
    RGB888 = 1,
    BGRA = 2,
    YUYV = 3,
    NV21 = 4,
    I420 = 5
} SCVColorFormat;

typedef enum {
    SUCCESS = 0,
    ERROR_INIT_FAILED = -1,
    ERROR_DEVICE_NOT_FOUND = -2,
    ERROR_STREAM_NOT_AVAILABLE = -3,
    ERROR_INVALID_PARAMETER = -4,
    ERROR_TIMEOUT = -5,
    ERROR_DEVICE_DISCONNECTED = -6
} SCVErrorCode;

struct ColorProfile{
    int width = 640;
    int height = 480;
    int fps = 15;
    SCVColorFormat format = RGB888;
};

struct DepthProfile{
    int width = 640;
    int height = 400;
    int fps = 15;
};

struct DeviceInfo {
    std::string name;
    std::string serialNumber;
    std::string firmwareVersion;
    bool isConnected;
};

// 回调函数类型定义
using FrameCallback = std::function<void(const cv::Mat& color, const cv::Mat& depth)>;
using ErrorCallback = std::function<void(SCVErrorCode error, const std::string& message)>;

class scvCameraCore {
public:
    scvCameraCore();
    explicit scvCameraCore(const std::string& deviceSerial);
    ~scvCameraCore();

    // 基本控制接口
    SCVErrorCode initialize();
    SCVErrorCode initialize(const ColorProfile& colorProfile, const DepthProfile& depthProfile);
    void shutdown();
    bool isInitialized() const { return initialized_; }
    bool isRunning() const { return running_; }

    // 设备管理
    static std::vector<DeviceInfo> enumerateDevices();
    SCVErrorCode connectDevice(const std::string& deviceSerial = "");
    void disconnectDevice();
    DeviceInfo getCurrentDeviceInfo() const;

    // 流控制
    SCVErrorCode startStreams();
    void stopStreams();
    SCVErrorCode restartStreams();

    // 帧获取接口
    SCVErrorCode getAlignColorDepthMat(cv::Mat& colorMat, cv::Mat& depthMat);
    SCVErrorCode getColorMat(cv::Mat& colorMat);
    SCVErrorCode getDepthMat(cv::Mat& depthMat);
    
    // 配置接口
    SCVErrorCode setColorProfile(const ColorProfile& profile);
    SCVErrorCode setDepthProfile(const DepthProfile& profile);
    ColorProfile getColorProfile() const { return colorProfile_; }
    DepthProfile getDepthProfile() const { return depthProfile_; }
    
    // 回调接口
    void setFrameCallback(const FrameCallback& callback);
    void setErrorCallback(const ErrorCallback& callback);
    void removeFrameCallback();
    void removeErrorCallback();

    // 状态查询
    bool isColorStreamEnabled() const;
    bool isDepthStreamEnabled() const;
    int getColorFPS() const { return cFPS_; }
    int getDepthFPS() const { return dFPS_; }
    
    // 工具函数
    static std::string getErrorString(SCVErrorCode error);
    void setLogLevel(int level);

private:
    // 核心成员
    std::shared_ptr<ob::Context> context_;
    std::shared_ptr<ob::Config> config_;
    std::shared_ptr<ob::Pipeline> pipe_;
    std::shared_ptr<ob::Device> device_;
    std::shared_ptr<ob::StreamProfileList> colorProfiles_;
    std::shared_ptr<ob::StreamProfileList> depthProfiles_;
    std::shared_ptr<ob::Align> align_;
    
    // 配置
    ColorProfile colorProfile_;
    DepthProfile depthProfile_;
    std::string deviceSerial_;
    
    // 状态
    bool initialized_ = false;
    bool running_ = false;
    int cFPS_ = 0;
    int dFPS_ = 0;
    
    // 回调
    FrameCallback frameCallback_;
    ErrorCallback errorCallback_;
    
    // 私有方法
    SCVErrorCode initProfile();
    SCVErrorCode initProfile(const ColorProfile& cProfile, const DepthProfile& dProfile);
    void processFrame(const std::shared_ptr<ob::Frame>& framePtr, cv::Mat& dst);
    void handleError(SCVErrorCode error, const std::string& message);
    bool validateProfile(const ColorProfile& profile);
    bool validateProfile(const DepthProfile& profile);
    OBFormat getOBFormat(SCVColorFormat format);
};


#endif //ORBBECSDK_SAMPLES_SCVCAMERACORE_H
